package com.neutech.maplestory.entity;

import com.neutech.maplestory.client.MapleStoryClient;
import com.neutech.maplestory.constant.Constant;
import com.neutech.maplestory.util.ImageUtil;
import com.neutech.maplestory.util.MusicThread;

import java.awt.*;
import java.util.List;

/**
 * 弓箭类
 * @author Neuedu-远哥
 * @version 1.0
 * @date 2021/11/30 14:08
 */
public class Arrow extends AbstractMapleStoryObject {



    public Arrow(){
        this.img = ImageUtil.getImageByKey("arrow");
        this.width = img.getWidth(null);
        this.height = img.getHeight(null);
        this.speed = 30;
    }

    public Arrow(MapleStoryClient msc,int x, int y, Direction dir){
        this();
        this.msc = msc;
        this.x = x;
        this.y = y;
        this.dir = dir;
    }

    @Override
    public void draw(Graphics g) {
        if(!live){
            this.msc.arrows.remove(this);
            return;
        }
        g.drawRect(x,y,width,height);
        switch (dir){
            case LEFT:
                g.drawImage(img,x,y,null);
                break;
            case RIGHT:
                Graphics2D graphics2D = (Graphics2D)g;
                graphics2D.drawImage(img, ImageUtil.flipHorizontal(img,x,y),null);
                break;
            default:
                break;
        }
        move();
    }

    @Override
    public void move() {
        switch (dir){
            case LEFT:
                this.x -= this.speed;
                break;
            case RIGHT:
                this.x += this.speed;
                break;
            default:
                break;
        }
        // 出界移除
        outOfBounds();
    }

    /**
     * 射击怪的方法
     * @param mob 被打的怪物对象
     * @return 是否打到
     */
    private boolean hit(Mob mob){
        if(this.live && this.getRectangle().intersects(mob.getRectangle()) && mob.action != Action.DIE){
            this.live = false;
            mob.hp -= msc.hero.attack;

            new MusicThread("Damage.mp3").start();

            Power power = new Power(msc,mob.x,mob.y);
            msc.powers.add(power);

            if(mob.hp <= 0){
                new MusicThread("Die.mp3").start();
                mob.action = Action.DIE;
                if(r.nextInt(101) >= 0){
                    // 掉道具
                    Item item = new Item(msc,mob.x,mob.y,r.nextInt(6) + 1);
                    this.msc.items.add(item);
                }
            }

            /*mob.live = false;*/
            return true;
        }
        return false;
    }

    /**
     * 击打一系列的怪
     * @param mobs
     * @return
     */
    public  boolean hit(List<Mob> mobs){
        for (int i = 0; i < mobs.size(); i++) {
            Mob mob = mobs.get(i);
            if(this.hit(mob)){
                return true;
            }
        }
        return false;
    }

    /**
     * 出界问题
     */
    public void outOfBounds(){
        if(this.x <= -500 || this.x >= Constant.WINDOW_WIDTH + 500){
            this.live = false;
        }
    }

    @Override
    public Rectangle getRectangle() {
        return new Rectangle(x,y,width,height);
    }
}
